package com.pipayshop.service.impl;

import com.pipayshop.domain.StoreFollowFocus;
import com.pipayshop.domain.vo.PageDataVO;
import com.pipayshop.domain.vo.StoreInfoVO1;
import com.pipayshop.domain.vo.StoreUserFollowInfoVO;
import com.pipayshop.mapper.PhysicalStoreFollowFocusMapper;
import com.pipayshop.service.PhysicalStoreFollowFocusService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 粉丝关注表 服务实现类
 * </p>
 *
 * @author jjf
 * @since 2023-07-29
 */
@Service
public class PhysicalStoreFollowFocusServiceImpl extends ServiceImpl<PhysicalStoreFollowFocusMapper, StoreFollowFocus> implements PhysicalStoreFollowFocusService {

    @Resource
    private PhysicalStoreFollowFocusMapper shopFollowFocusMapper;

    /**
     * 关注实体店
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean userFollowItem(String followId, String shopId) {
        // 查询有没有曾经关注过，有就直接就是吧status字段改为0
        StoreFollowFocus shopFollowFocus = shopFollowFocusMapper.selectOne(new QueryWrapper<StoreFollowFocus>()
                .eq("shop_id", shopId)
                .eq("follow_id", followId)
                .eq("status", 1));
        if (shopFollowFocus == null){
            StoreFollowFocus shopFollow = new StoreFollowFocus();
            shopFollow.setFollowId(followId);
            shopFollow.setShopId(shopId);
            int insert = shopFollowFocusMapper.insert(shopFollow);
            return insert > 0;
        }
        int update = shopFollowFocusMapper.update(null, new UpdateWrapper<StoreFollowFocus>()
                .eq("shop_id", shopId)
                .eq("follow_id", followId)
                .set("status", 0)
                .set("update_time", new Date()));
        return update > 0;
    }

    /**
     * 用户取消关注
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean userUnfollow(String followId, String shopId) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        int result = shopFollowFocusMapper.update(null, new UpdateWrapper<StoreFollowFocus>()
                .eq("shop_id", shopId)
                .eq("follow_id", followId)
                .set("update_time", format)
                .set("status", 1));
        return result>0;
    }


    /**
     * 获取粉丝列表
     */
    @Override
    public PageDataVO getFollowList(String shopId, Integer pageNum, Integer pageSize) {
        List<StoreUserFollowInfoVO> result = shopFollowFocusMapper.shopFollowFocusList(shopId, (pageNum - 1)*pageSize, pageSize);
        Long count = shopFollowFocusMapper.getCount(shopId);
        return new PageDataVO(count.intValue(), result);
    }

    @Override
    public boolean isFollowShop(String shopId, String userId) {
        int i = shopFollowFocusMapper.selectCount(new QueryWrapper<StoreFollowFocus>().eq("shop_id", shopId)
                .eq("follow_id", userId).eq("status", 0)).intValue();
        return i == 1;
    }

    @Override
    public Integer SelectFollowShopSum(String shopId) {
        return shopFollowFocusMapper.selectCount(new QueryWrapper<StoreFollowFocus>().eq("shop_id", shopId).eq("status", 0)).intValue();
    }
    /**
     * 根据用户id查询用户关注的实体店列表
     */
    @Override

    public PageDataVO getFollowList (Integer page,Integer limit,String userId){
        Integer integer = shopFollowFocusMapper.selectAllFollowProductByUserId(userId);
        List<StoreInfoVO1> shopInfos = shopFollowFocusMapper.selectFollowProductByUserId((page - 1) * limit, limit, userId);
        return new PageDataVO(integer,shopInfos);
    }
}
